import Vue from "vue";
import Vuex from "vuex";
import {
	setLoginStatus
} from '../utils/index.js'

Vue.use(Vuex)

const state = {
	isLogin: false,
	userInfo: {},
	connection: {},
	gather: {}, // 群聊列表
	chatList: {},
	timer: null,
};

const store = new Vuex.Store({
	state,
	mutations: {
		init(state, polo) {
			state.isLogin = false
			state.userInfo = {}
			state.connection = {};
			state.gather = {};
			state.chatList = {};
			clearInterval(state.timer)
			state.timer = null
			console.log('清空了')
		},
		setLogin(state, isLogin) {
			state.isLogin = isLogin
			setLoginStatus(isLogin)
			if (!isLogin) {
				state.userInfo = {}
				state.chatList = {}
			}
		},
		setTimer(state, timer) {
			state.timer = timer
		},
		setLocationStatus(state, location_status) {
			state.userInfo["location_status"] = location_status
			uni.setStorageSync('userInfo', state.userInfo)
			uni.setStorageSync('activity', !!location_status)
		},
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo
			uni.setStorageSync('userInfo', userInfo)
		},
		setChatList(state, list) {
			state.chatList = list || {}
		},
		setChat(state, playload) {
			state.chatList[playload.lost_id] = state.chatList[playload.lost_id] || []
			state.chatList[playload.lost_id].push(playload.chat)
		},
		setConnection(state, connection) {
			state.connection = connection
		},
		setGather(state, gather) {
			state.gather = gather
		},
		setGatherNumber(state, playload) {
			state.gather[playload.lost_id].number = playload.number
		}
	}
})

export default store
